import { ClientMessage } from "@runtime/generate/message/ClientMessage";
import WebSocketComponent from "@runtime/module/WebSocket/WebSocketComponent";

/**
 * 天空盒组件
 */
export default class SkyboxComponent {
    private static instance: SkyboxComponent = new SkyboxComponent();
    /**
     * 天空盒组件
     */
    public static get Default(): SkyboxComponent {
        return this.instance;
    }

    constructor() {
        window.addEventListener('unload', this.Dispose.bind(this));
    }

    Dispose() {
        window.removeEventListener('unload', this.Dispose);
    }

    public async LoadAsync(asset: string, clearFlags: number = 1): Promise<boolean> {
        const response: ClientMessage.LoadSkyboxResponse = await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.LoadSkyboxRequest(asset, clearFlags));
        return response.Error == 0;
    }
}